package com.tcxhb.job.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcxhb.job.dao.dataobject.JobAppDO;
import com.tcxhb.job.dao.dataobject.query.JobAppQuery;
import com.tcxhb.job.dao.mapper.JobAppMapper;
import com.tcxhb.job.dao.repository.JobAppRepository;
import com.tcxhb.job.common.model.PageResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * author:auto.generator
 * time: 2023-11-02
 */
@Repository
public class JobAppRepositoryImpl extends ServiceImpl<JobAppMapper, JobAppDO> implements JobAppRepository {

    @Override
    public Long create(JobAppDO jobAppDO) {
        super.save(jobAppDO);
        return jobAppDO.getId();
    }

    @Override
    public boolean deleteById(Long id) {
        return super.removeById(id);
    }

    @Override
    public boolean updateById(JobAppDO jobAppDO) {
        return super.updateById(jobAppDO);
    }

    @Override
    public JobAppDO queryById(Long id) {
        return super.getById(id);
    }

    @Override
    public PageResponse<JobAppDO> page(JobAppQuery query) {
        LambdaQueryWrapper<JobAppDO> wrapper = queryWrapper(query);
        Page<JobAppDO> page = new Page<>(query.getPage().getPageNum(), query.getPage().getPageSize());
        IPage response = page(page, wrapper);
        PageResponse result = new PageResponse();
        result.setTotal(response.getTotal());
        result.setList(response.getRecords());
        return result;
    }

    @Override
    public List<JobAppDO> list(JobAppQuery query) {
        LambdaQueryWrapper<JobAppDO> wrapper = queryWrapper(query);
        return super.list(wrapper);
    }

    private LambdaQueryWrapper<JobAppDO> queryWrapper(JobAppQuery query) {
        LambdaQueryWrapper<JobAppDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getId() != null, JobAppDO::getId, query.getId());
        if (StringUtils.isNotBlank(query.getAppName())) {
            wrapper.eq(JobAppDO::getAppName, query.getAppName());
        }
        return wrapper;
    }
}
